package com.clgk.wechatmall.manager.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.clgk.wechatmall.common.sql.CommonDaoImpl;
import com.clgk.wechatmall.common.sql.Page;
import com.clgk.wechatmall.common.sql.SqlUtils;
import com.clgk.wechatmall.common.util.Md5Util;
import com.clgk.wechatmall.manager.dao.ManagerDao;
import com.clgk.wechatmall.manager.entity.ManagerVo;

@Repository
public class ManagerDaoImpl extends CommonDaoImpl implements ManagerDao {

	Logger logger = Logger.getLogger(ManagerDaoImpl.class);

	@Override
	public List<ManagerVo> getUserList(ManagerVo vo,Page page) {
		String sql = "SELECT t.*,tr.POWER ,tp.`PARTNERNAME`,tc.`NAME` cityname ";
		sql += " FROM T_MANAGER t ";
		sql += " LEFT JOIN lampserver.`T_ROLE` tr ON t.`ROLEID` = tr.ID ";
		sql += " LEFT JOIN lampserver.`T_PARTNER` tp ON t.`PARTNERID` = tp.`ID` ";
		sql += " LEFT JOIN lampserver.`T_CITY` tc ON tp.`CITYID` = tc.`ID` ";
		sql += "  WHERE 1=1 ";
		List<Object> params = new ArrayList<Object>();
		
		if(StringUtils.isNotEmpty(vo.getAccount())){
			sql += " AND t.ACCOUNT=? ";
			params.add(vo.getAccount());
		}
		if(vo.getType() != null){
			sql += " AND t.TYPE=? ";
			params.add(vo.getType());
		}
		
		if(vo.getPartnerid()!=null){
			sql += " AND t.PARTNERID=? ";
			params.add(vo.getPartnerid());
		}
		if(vo.getParentid() != null){
			sql += " AND t.PARENTID=? ";
			params.add(vo.getParentid());
		}
		return this.query(sql, ManagerVo.class, params,page);
	}

	@Override
	public Long saveUser(ManagerVo vo) {
		String psd = vo.getPassword();
		vo.setPassword(Md5Util.MD5(Md5Util.MD5(psd)));
		String sql = SqlUtils.fullInsertForBeanSql(vo);
		logger.info("saveUser sql:"+sql);
		Long id = this.insert(sql, vo);
		sql = " INSERT INTO `lampserver`.`T_MANAGER_INIT`(`MANAGERID`,`PASSWORD`,`CREATETIME`) VALUES ("+id+",'"+psd+"',NOW()) ";
		this.update(sql,new Object[]{});
		return id;
	}

	@Override
	public Long updateUser(ManagerVo vo) {
		if(StringUtils.isNotBlank(vo.getPassword())){
			String sql = "UPDATE lampserver.`T_MANAGER_INIT` SET PASSWORD = ? WHERE MANAGERID=?";
			this.update(sql,new Object[]{vo.getPassword(),vo.getId()});
			vo.setPassword(Md5Util.MD5(Md5Util.MD5(vo.getPassword())));
		}
		return (long)this.update(SqlUtils.fullUpdateForBeanSql(vo), vo);
	}

	@Override
	public Integer delUser(String ids) {
		String sql = "DELETE FROM T_MANAGER WHERE ID IN ("+ids + ")";
		return this.update(sql, new Object[]{});
	}

	@Override
	public ManagerVo getUser(String account, String pwd) {
		String sql = "SELECT t.*,tr.POWER ,tp.`PARTNERNAME`,tc.`NAME` cityname,tc.`ID` cityid ";
		sql += " FROM T_MANAGER t  ";
		sql += " LEFT JOIN lampserver.`T_ROLE` tr ON t.`ROLEID` = tr.ID ";
		sql += " LEFT JOIN lampserver.`T_PARTNER` tp ON t.`PARTNERID` = tp.`ID` ";
		sql += " LEFT JOIN lampserver.`T_CITY` tc ON tp.`CITYID` = tc.`ID` ";
		sql += " WHERE ACCOUNT=? AND PASSWORD=?";
		return (ManagerVo) this.getBean(sql, ManagerVo.class, new Object[]{account,pwd});
	}
	
}
